// pages/Index/bill/bill.js
const controllers = require('../../controllers/controllerIndex').controller
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    billList: [
      // {price:1,name:2},{price:1,name:3},{price:1,name:4}
    ],
    time: '',
    shopId: '',
    choiceactive: 0,
    way: 0,
    endTime: '',
    canPay:true
  },
  //选择方式
  choice(e) {
    this.setData({
      choiceactive: e.currentTarget.dataset.index,
      way: e.currentTarget.id
    })
  },
  //提交续费订单
  ok() {
    let that=this
    if(this.data.canPay==false){
      return
    }
    this.setData({
      canPay:false
    })
    controllers.comfirmBill({
      renewId: this.data.way,
      shopId: wx.getStorageSync('shopId')
    }).then(res => {
      let orderNo = res.data.orderNo
      let accountId = res.data.accountId
      let orderId = res.data.id
      controllers.weChatPay({
        shopId: wx.getStorageSync('shopId'),
        orderNo: orderNo,
        frontUrl: "mp:///pages/Index/Index",
        type: "SP",
        openId: wx.getStorageSync('openId')
      }).then(res => {
        console.log(res);
        // wx.navigateTo({
        //   url: '/pages/weixinlink/weixinlink?url='+encodeURIComponent(res.data)
        // })
        let data = JSON.parse(res.data);
        console.log(data)
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          timeStamp: data.timeStamp,
          signType: data.signType,
          success(res) {
            wx.navigateBack()
          },
          fail(res) {
            console.log(res)
          },
          complete(){
            that.setData({
              canPay:true
            })
          }
        })
      })

    })
  },

  //处理时间

  dataFunction(num,data) {
    var d2 = ""
    if (data) {
      let relData=data.replace(/-/g, "/")
      d2 = new Date(relData);
    } else {
      d2 = new Date();
    }
    let m = d2.getMonth() + num;
    let y = d2.getFullYear();
    if (m > 11) {
      y += m / 12;
      m -= 12;
    }
    d2.setMonth(m);
    d2.setFullYear(y);
    return d2.getFullYear()+"/"+(d2.getMonth() + 1)+"/"+d2.getDate();
  },

  //获取店铺首页信息
  getShopMsg() {
    let that = this
    controllers.shopInfo({
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId')
    }).then(res => {

      let newTime = new Date().getTime();
      let lastDay = 0;
      if (res.data.endTime) {

        let endTime = new Date(res.data.endTime.replace(/-/g, "/")).getTime();
        lastDay = Math.ceil((endTime - newTime) / 1000 / 24 / 3600);


        that.setData({
          lastDay
        })
        console.log(lastDay)
        wx.setStorageSync('endTime', res.data.endTime)
        wx.setStorageSync('shopGroupEndTime', res.data.shopGroupEndTime)
      }
      that.setData({
        storeInfo: res.data,

      })
      wx.setStorageSync('typeId', res.data.typeId)

      if(res.data.shopGroupEndTime){
        let shopGroupEndTime=res.data.shopGroupEndTime.replace(/-/g, "/")
        if (shopGroupEndTime) {
          if (new Date(shopGroupEndTime).getTime() > newTime) {
            this.setData({
              Effective: true
            })
            wx.setStorageSync('Effective', true)
          } else {
            this.setData({
              Effective: false
            })
            wx.setStorageSync('Effective', false)
          }
        }
      }else {
        this.setData({
          Effective: false
        })
        wx.setStorageSync('Effective', false)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: wx.getStorageSync('shopId'),
      endTime: wx.getStorageSync('endTime').slice(0, 10),
      shopGroupEndTime: wx.getStorageSync('shopGroupEndTime') ? wx.getStorageSync('shopGroupEndTime').slice(0, 10) : "",
      type: parseInt(options.type)
    })
    //获取续费列表
    controllers.getBillList({
      type: this.data.type
    }).then(res => {
      let list = res.data
      if (this.data.type === 1) {
        if (wx.getStorageSync('Effective')) {
          let endTime = wx.getStorageSync('shopGroupEndTime').replace(/-/g, '/')
          list.forEach(item => {
            item.canUserTime = this.dataFunction(item.effectiveTime,endTime)
          })
        } else {
          list.forEach(item => {
            item.canUserTime = this.dataFunction(item.effectiveTime)
          })
        }
      } else {
        if (this.data.endTime) {
          let endTime = wx.getStorageSync('endTime').replace(/-/g, '/')
          list.forEach(item => {
            item.canUserTime = this.dataFunction(item.effectiveTime,endTime)
          })
        } else {
          list.forEach(item => {
            item.canUserTime = this.dataFunction(item.effectiveTime)
          })
        }
      }
      this.setData({
        billList: res.data,
        way: res.data[0].id
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getShopMsg()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})